package com.agd.quick.states;
//Q
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.color.Color;

import android.view.KeyEvent;
import lib.elementgame.GameAnim;
import lib.elementgame.GameSprite;
import lib.engine.Anchor;
import lib.engine.GameEngine;
import lib.engine.GameState;

public class StateQuit extends GameState {

	private static GameSprite backgroundFloor;
	private static GameSprite background;
	private static GameSprite background2;
	private static GameSprite background3;
	private Rectangle bg_game;
	private static GameAnim btnBack;
	private static GameAnim btnYes;
	private boolean keluar;
	public StateQuit(GameEngine engine) {
		super(engine);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		if (pSceneTouchEvent.isActionDown()) {
			if (pTouchArea == btnBack) {
				btnBack.setCurrentTileIndex(1);
			}
			if (pTouchArea == btnYes) {
				btnYes.setCurrentTileIndex(1);
			}
		}
		if (pSceneTouchEvent.isActionUp()) {
			if (pTouchArea == btnBack) {
				btnBack.setCurrentTileIndex(0);
				exitState(MainMenuState);
			}
			if (pTouchArea == btnYes) {
				btnYes.setCurrentTileIndex(0);
				engine.finish();
			}
			else{
				btnYes.setCurrentTileIndex(0);
				btnBack.setCurrentTileIndex(0);
			}
		}
		return false;
	}

	@Override
	public void initComponent() {
		// TODO Auto-generated method stub
		background = new GameSprite(GP_SPRITE_BACKGGROUND, engine);
		background2 = new GameSprite(GP_SPRITE_BACKGGROUND2, engine);
		background3 = new GameSprite(GP_SPRITE_BACKGGROUND3, engine);
		backgroundFloor = new GameSprite(GQ_SPRITE_BACKGROUND, engine);
		bg_game = new Rectangle(0, 0, 800, 480, engine.getVertexBufferObjectManager());
		btnBack = new GameAnim(GQ_ANIM_BTNBACK, engine);
		btnYes = new GameAnim(GQ_ANIM_BTNYES, engine);

	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub
		background.setWidth(800);
		background.setHeight(550);
		background2.setWidth(800);
		background2.setHeight(450);
		background3.setWidth(800);
		background3.setHeight(450);
		backgroundFloor.setWidth(800);
		keluar = false;
		bg_game.setColor(Color.WHITE);
        bg_game.setAlpha(0.1f);
	}

	@Override
	protected void attach() {
		// TODO Auto-generated method stub
		engine.scene.attachChild(background3);
		engine.scene.attachChild(background2);
		engine.scene.attachChild(background);
		engine.scene.attachChild(bg_game);
		engine.scene.attachChild(backgroundFloor);
		engine.scene.attachChild(btnBack);
		engine.scene.attachChild(btnYes);
	}

	@Override
	protected void detach() {
		// TODO Auto-generated method stub
		background3.detachSelf();
		background2.detachSelf();
		background.detachSelf();
		backgroundFloor.detachSelf();
		btnBack.detachSelf();
		btnYes.detachSelf();
		bg_game.detachSelf();
	}

	@Override
	protected void setPosition() {
		// TODO Auto-generated method stub
		background.setPosition(Anchor.BOTTOM_LEFT);
		background2.setX(0);
		background2.setY(-20);
		background3.setX(0);
		background3.setY(-40);
		btnBack.setPosition(Anchor.CENTER);
		btnYes.setPosition(-40, 0, Anchor.CENTER_RIGHT);
	}

	@Override
	protected void registerTouch() {
		// TODO Auto-generated method stub
		engine.scene.registerTouchArea(btnBack);
		engine.scene.registerTouchArea(btnYes);
		engine.scene.registerTouchArea(background);
		engine.scene.registerTouchArea(background2);
		engine.scene.registerTouchArea(background3);
	}

	@Override
	protected void unregisterTouch() {
		// TODO Auto-generated method stub
		engine.unregisterSceneTouch(btnBack);
		engine.unregisterSceneTouch(btnYes);
		engine.unregisterSceneTouch(background);
		engine.unregisterSceneTouch(background2);
		engine.unregisterSceneTouch(background3);
	}

	@Override
	protected void onUpdate() {
		// TODO Auto-generated method stub
		if(keluar){
			exitState(MainMenuState);
		}
	}

	@Override
	protected void onPaused() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onResumed() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			keluar = true;
		}
	}

	@Override
	public void initLoading() {
		// TODO Auto-generated method stub
		
	}

}
